#include <NDL.h>
#include <SDL.h>
#include <string.h>
#define keyname(k) #k,
static const char *keyname[] = {
  "NONE",
  _KEYS(keyname)
};
uint8_t key_state [200];
int SDL_PushEvent(SDL_Event *ev) {
  printf("function SDL_PushEvent but cannt realization\n");
  return 0;
}
int SDL_PollEvent(SDL_Event *ev) {
  char buf[64];
  int ev_num = 0;
  if(ev==NULL) return 1;
  ev_num = NDL_PollEvent(buf, sizeof(buf));
  if (ev_num == 0)
  {
    return 0 ;
  }
  char key_type [8];
  char key_name [8] = "NONE";
  int  state;
  sscanf(buf,"%s %s",key_type,key_name);
  
  if (strcmp(key_type,"kd") == 0)
  {
    ev->type = SDL_KEYDOWN;
    state    = 1;
  }
  else if (strcmp(key_type,"ku") == 0)
  {
    ev->type = SDL_KEYUP;
    state    = 0;
  }
  int i = 0;
  while (1)
  {
    if (strcmp(keyname[i],key_name) == 0)
    {
      ev->key.keysym.sym = i;
      key_state[i] = state;
      break;
    }
    i++;
  }
  return 1;
}
int SDL_WaitEvent(SDL_Event *event) {
  char buf[64];
  int ev = 0;
  while (1)
  {
    ev = NDL_PollEvent(buf, sizeof(buf));
    if (ev > 0)
    {
      break;
    }
  }
  char key_type [8];
  char key_name [8] = "NONE";
  sscanf(buf,"%s %s",key_type,key_name);
  if (strcmp(key_type,"kd") == 0)
  {
    event->type = SDL_KEYDOWN;
  }
  else if (strcmp(key_type,"ku") == 0)
  {
    event->type = SDL_KEYUP;
  }
  int i = 0;
  while (1)
  {
    if (strcmp(keyname[i],key_name) == 0)
    {
      event->key.keysym.sym = i;
      break;
    }
    i++;
  }
  return 1;
}

int SDL_PeepEvents(SDL_Event *ev, int numevents, int action, uint32_t mask) {
  printf("function SDL_PeepEvents but cannt realization\n");
  return 0;
}

uint8_t* SDL_GetKeyState(int *numkeys) {
  // printf("function SDL_GetKeyState but cannt realization");
  
  return &key_state[0];
}
